//
//  WCBAdressListTableViewCell.m
//  WeChatBuniess
//
//  Created by TFAppleWork-Summer on 2017/12/14.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "WCBAdressListTableViewCell.h"
#import "UIScreen+Extend.h"
#import "UIColor+Extend.h"
#import "UIView+Layout.h"
#import "WCBAdressModel.h"
#import "WCBAdressViewModel.h"

@interface WCBAdressListTableViewCell()

/**
 名称
 */
@property(nonatomic,strong) UILabel *nameLabel;

/**
 手机号
 */
@property (nonatomic,strong) UILabel *phoneLabel;

/**
 地址
 */
@property (nonatomic,strong) UILabel *adressLabel;

/**
 选中按钮
 */
@property (nonatomic,strong) UIButton *selectBtn;

/**
 横线
 */
@property(nonatomic,strong) UIView *lineView;

@end
@implementation WCBAdressListTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.phoneLabel];
        [self.contentView addSubview:self.adressLabel];
        [self.contentView addSubview:self.selectBtn];
        [self.contentView addSubview:self.lineView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}



-(void)setViewModel:(WCBAdressViewModel *)viewModel{
    _viewModel = viewModel;
    self.nameLabel.text = viewModel.model.contacts;
    self.nameLabel.frame = CGRectMake(20, 20, 0, 0);
    [self.nameLabel sizeToFit];
    
    self.phoneLabel.text = viewModel.model.contactsPhone;
    self.phoneLabel.frame = CGRectMake(self.nameLabel.right + 20, self.nameLabel.y , 0,0 );
    [self.phoneLabel  sizeToFit ];
    
    if ([self.viewModel.model.isdefault isEqualToString:@"1"]) {
        NSString *adressString = [NSString stringWithFormat:@"[默认地址]%@",self.viewModel.model.addressDesc];
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWithString:adressString];
        [att addAttributes:@{NSForegroundColorAttributeName :[UIColor redColor]} range:NSMakeRange(0, 6)];
        self.adressLabel.attributedText =att;
    }
    else{
         self.adressLabel.text = self.viewModel.model.addressDesc;
    }
    if (self.viewModel.isSelect) {
        [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"adressSelect"] forState:UIControlStateNormal];
    }
    else{
    
        [self.selectBtn setBackgroundImage:[UIImage imageNamed:@"adressNotSelect"] forState:UIControlStateNormal];
    }
    self.adressLabel.frame = CGRectMake(self.nameLabel.x, self.nameLabel.bottom + 10, [UIScreen width] -90, self.viewModel.adressHeight);
    [self.adressLabel sizeToFit];
    
    self.lineView.frame = CGRectMake(0, (70 + self.viewModel.adressHeight)-0.5, [UIScreen width], 0.5);
}

- (void)selectBtnClick:(UIButton *)button{
    
    self.viewModel.isSelect = YES;
    if (self.selectBlock) {
        self.selectBlock(self.viewModel);
    }
}


#pragma mark - lazy-load
-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc ]init];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.font = [UIFont systemFontOfSize:23];
    }
    return _nameLabel;
}

-(UILabel *)phoneLabel{
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc]init];
        _phoneLabel.font = [UIFont systemFontOfSize:23];
        _phoneLabel.textColor = [UIColor blackColor];
    }
    return _phoneLabel;
}

-(UILabel *)adressLabel{
    if (!_adressLabel) {
        _adressLabel = [[UILabel alloc]init];
        _adressLabel.font = [UIFont systemFontOfSize:23];
        _adressLabel.numberOfLines = 0;
    }
    return _adressLabel;
    
}

-(UIButton *)selectBtn{
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.frame = CGRectMake([UIScreen width] - 60, 35, 30, 30);
        _selectBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]initWithFrame: CGRectMake(0, (70 + self.viewModel.adressHeight)-0.5, [UIScreen width], 0.5)];
        _lineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _lineView;
}
@end
